﻿using System.Collections.Generic;
using System.Linq;

namespace Hyt.Core.Cache
{
    public class CacheKey
    { 
        /// <summary>
        /// 缓存提供者
        /// </summary>
        public ICacheProvider Provider;

        public string FullKey { get; set; }

        private string Key { get; set; }

        public CacheKey NameSpace { get;  set; }

        private bool _inited = false;

        public CacheKey(){}

        public CacheKey(string fullkey,ICacheProvider provider)
        {
            FullKey = fullkey;
            Provider = provider;
        }

        private void Init()
        {
            if (!_inited)
            {
                if (string.IsNullOrWhiteSpace(FullKey)) return;
                FullKey = FullKey.Trim().ToLower();
                string[] arr = FullKey.Split('.');
                Key = arr[arr.Length - 1];
                if (arr.Length > 1)
                {
                    var list = new List<string>(arr);
                    list.RemoveAt(list.Count() - 1);
                    NameSpace = new CacheKey(string.Join(".", list.ToArray()), this.Provider);
                }
            }
            _inited = true;
        }

        /// <summary>
        /// 获取GUID
        /// </summary>
        /// <returns></returns>
        private string getguid()
        {
            return System.Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// 获取KEY
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string getkey(string key)
        {
            var value = Provider.GetData(key);
            if(value==null)
            {
                value = getguid();
                Provider.Set(key, value);
            }
            return value.ToString();
        }

        /// <summary>
        /// 解析本身
        /// </summary>
        /// <param name="parseself"></param>
        /// <returns></returns>
        public string GetCacheKey(bool parseself=false)
        {
            Init();
            if (NameSpace == null)
            {
                if (!parseself)
                {
                    return FullKey;
                }
                else
                {
                    return getkey(FullKey);
                }
            }
            string key = NameSpace.GetCacheKey(true);
            if (!parseself)
            {
                return key + "." + this.Key;
            }
            else
            {
                string selfkey = "";
                if (this.Key[0] == '[' && this.Key[this.Key.Length - 1] == ']')
                {
                    selfkey = this.Key;
                }
                else
                {
                    selfkey = key+"."+this.Key;
                    
                }
                var result=getkey(key + "." + getkey(selfkey));
                return result;
            }
        }
    }

}
